/*
 * Copyright (c) 2011, Michael van der Westhuizen
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *    - Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *    - Redistributions in binary form must reproduce the above
 *      copyright notice, this list of conditions and the following
 *      disclaimer in the documentation and/or other materials provided
 *      with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
#ifndef PYXMLSEC1_KEYSMNGR_C_MCVDW_20110104
#define PYXMLSEC1_KEYSMNGR_C_MCVDW_20110104

#include "pyxmlsec.h"

#include <xmlsec/keysmngr.h>



static PyObject * xmlsec1_xmlSecKeysMngrCreate(PyObject * self, PyObject * args)
{
    xmlSecKeysMngrPtr ptr = NULL;

    if (NULL == (ptr = xmlSecKeysMngrCreate()))
    {
        ENSURE_EXCEPTION_SET(xmlSecKeysMngrCreate);
        return NULL;
    }

    return xmlsec1mod_wrap_xmlSecKeysMngrPtr(ptr);
}



static PyObject * xmlsec1_xmlSecKeysMngrDestroy(PyObject * self, PyObject * arg)
{
    xmlSecKeysMngrPtr ptr;

    if (NULL == (ptr = xmlsec1mod_unwrap_xmlSecKeysMngrPtr(arg)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecKeysMngrPtr);
        return NULL;
    }

    xmlSecKeysMngrDestroy(ptr);
    Py_RETURN_NONE;
}



static PyObject * xmlsec1_xmlSecKeysMngrFindKey(PyObject * self, PyObject * args, PyObject * kwds)
{
    PyObject * arg1;
    PyObject * arg2;
    PyObject * arg3;
    xmlSecKeysMngrPtr mngr;
    xmlChar * name;
    xmlSecKeyInfoCtxPtr keyInfoCtx;
    xmlSecKeyPtr ptr;
    PyObject * ret;

    static char * kwlist[] = {"mngr", "name", "keyInfoCtx", NULL};

    arg1 = arg2 = arg3 = NULL;
    mngr = NULL;
    name = NULL;
    keyInfoCtx = NULL;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOO", kwlist, &arg1, &arg2, &arg3))
    {
        return NULL;
    }

    if (NULL == (mngr = xmlsec1mod_unwrap_xmlSecKeysMngrPtr(arg1)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecKeysMngrPtr);
        return NULL;
    }

    if (NULL == (keyInfoCtx = xmlsec1mod_unwrap_xmlSecKeyInfoCtxPtr(arg3)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecKeyInfoCtxPtr);
        return NULL;
    }

    if (0 != xmlsec1_UnicodeOrBytesToXmlCharPtr(arg2, &name, NULL))
    {
        ENSURE_EXCEPTION_SET(xmlsec1_UnicodeOrBytesToXmlCharPtr);
        return NULL;
    }

    if (NULL == (ptr = xmlSecKeysMngrFindKey(mngr, name, keyInfoCtx)))
    {
        xmlFree(name);

        if (!PyErr_Occurred())
        {
            Py_RETURN_NONE;
        }

        ENSURE_EXCEPTION_SET(xmlSecKeysMngrFindKey);
        return NULL;
    }

    xmlFree(name);

    if (NULL == (ret = xmlsec1mod_wrap_xmlSecKeyPtr(ptr)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_wrap_xmlSecKeyPtr);
        xmlSecKeyDestroy(ptr);
        return NULL;
    }

    return ret;
}



static PyObject * xmlsec1_xmlSecKeysMngrAdoptKeysStore(PyObject * self, PyObject * args, PyObject * kwds)
{
    PyObject * arg1;
    PyObject * arg2;
    xmlSecKeysMngrPtr mngr;
    xmlSecKeyStorePtr store;

    static char * kwlist[] = {"mngr", "store", NULL};

    arg1 = arg2 = NULL;
    mngr = NULL;
    store = NULL;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist, &arg1, &arg2))
    {
        return NULL;
    }

    if (NULL == (mngr = xmlsec1mod_unwrap_xmlSecKeysMngrPtr(arg1)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecKeysMngrPtr);
        return NULL;
    }

    if (NULL == (store = xmlsec1mod_unwrap_xmlSecKeyStorePtr(arg2)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecKeyStorePtr);
        return NULL;
    }

    if (0 != xmlSecKeysMngrAdoptKeysStore(mngr, store))
    {
        ENSURE_EXCEPTION_SET(xmlSecKeysMngrAdoptKeysStore);
        return NULL;
    }

    Py_RETURN_NONE;
}



static PyObject * xmlsec1_xmlSecKeysMngrGetKeysStore(PyObject * self, PyObject * arg)
{
    xmlSecKeysMngrPtr mngr;
    xmlSecKeyStorePtr store;
    PyObject * ret;

    if (NULL == (mngr = xmlsec1mod_unwrap_xmlSecKeysMngrPtr(arg)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecKeysMngrPtr);
        return NULL;
    }

    if (NULL == (store = xmlSecKeysMngrGetKeysStore(mngr)))
    {
        if (!PyErr_Occurred())
        {
            Py_RETURN_NONE;
        }

        ENSURE_EXCEPTION_SET(xmlSecKeysMngrGetKeysStore);
        return NULL;
    }

    if (NULL == (ret = xmlsec1mod_wrap_xmlSecKeyStorePtr(store)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_wrap_xmlSecKeyStorePtr);
        return NULL;
    }

    return ret;
}



static PyObject * xmlsec1_xmlSecKeysMngrAdoptDataStore(PyObject * self, PyObject * args, PyObject * kwds)
{
    PyObject * arg1;
    PyObject * arg2;
    xmlSecKeysMngrPtr mngr;
    xmlSecKeyDataStorePtr store;

    static char * kwlist[] = {"mngr", "store", NULL};

    arg1 = arg2 = NULL;
    mngr = NULL;
    store = NULL;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist, &arg1, &arg2))
    {
        return NULL;
    }

    if (NULL == (mngr = xmlsec1mod_unwrap_xmlSecKeysMngrPtr(arg1)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecKeysMngrPtr);
        return NULL;
    }

    if (NULL == (store = xmlsec1mod_unwrap_xmlSecKeyDataStorePtr(arg2)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecKeyDataStorePtr);
        return NULL;
    }

    if (0 != xmlSecKeysMngrAdoptDataStore(mngr, store))
    {
        ENSURE_EXCEPTION_SET(xmlSecKeysMngrAdoptDataStore);
        return NULL;
    }

    Py_RETURN_NONE;
}



static PyObject * xmlsec1_xmlSecKeysMngrGetDataStore(PyObject * self, PyObject * args, PyObject * kwds)
{
    PyObject * arg1;
    PyObject * arg2;
    xmlSecKeysMngrPtr mngr;
    xmlSecKeyDataStoreId id;
    xmlSecKeyDataStorePtr ptr;
    PyObject * ret;

    static char * kwlist[] = {"mngr", "id", NULL};

    arg1 = arg2 = NULL;
    mngr = NULL;
    id = NULL;
    ptr = NULL;
    ret = NULL;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist, &arg1, &arg2))
    {
        return NULL;
    }

    if (NULL == (mngr = xmlsec1mod_unwrap_xmlSecKeysMngrPtr(arg1)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecKeysMngrPtr);
        return NULL;
    }

    if (NULL == (id = xmlsec1mod_unwrap_xmlSecKeyDataStoreId(arg2)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecKeyDataStoreId);
        return NULL;
    }

    if (NULL == (ptr = xmlSecKeysMngrGetDataStore(mngr, id)))
    {
        if (!PyErr_Occurred())
        {
            Py_RETURN_NONE;
        }

        ENSURE_EXCEPTION_SET(xmlSecKeysMngrGetDataStore);
        return NULL;
    }

    if (NULL == (ret = xmlsec1mod_wrap_xmlSecKeyDataStorePtr(ptr)))
    {
        ENSURE_EXCEPTION_SET(xmlSecKeyDataStorePtr);
        return NULL;
    }

    return ret;
}



static PyObject * xmlsec1_xmlSecKeysMngrGetKey(PyObject * self, PyObject * args, PyObject * kwds)
{
    PyObject * arg1;
    PyObject * arg2;
    xmlNodePtr keyInfoNode;
    xmlSecKeyInfoCtxPtr keyInfoCtx;
    xmlSecKeyPtr ptr;
    PyObject * ret;

    static char * kwlist[] = {"keyInfoNode", "keyInfoCtx", NULL};

    arg1 = arg2 = NULL;
    keyInfoNode = NULL;
    keyInfoCtx = NULL;
    ptr = NULL;
    ret = NULL;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist, &arg1, &arg2))
    {
        return NULL;
    }

    if (NULL == (keyInfoNode = xmlsec1mod_unwrap_xmlNodePtr(arg1)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlNodePtr);
        return NULL;
    }

    if (NULL == (keyInfoCtx = xmlsec1mod_unwrap_xmlSecKeyInfoCtxPtr(arg2)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecKeyInfoCtxPtr);
        return NULL;
    }

    if (NULL == (ptr = xmlSecKeysMngrGetKey(keyInfoNode, keyInfoCtx)))
    {
        if (!PyErr_Occurred())
        {
            Py_RETURN_NONE;
        }

        ENSURE_EXCEPTION_SET(xmlSecKeysMngrGetKey);
        return NULL;
    }

    if (NULL == (ret = xmlsec1mod_wrap_xmlSecKeyPtr(ptr)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_wrap_xmlSecKeyPtr);
        return NULL;
    }

    return ret;
}



static PyObject * xmlsec1_xmlSecKeyStoreCreate(PyObject * self, PyObject * arg)
{
    xmlSecKeyStoreId id;
    xmlSecKeyStorePtr ptr;
    PyObject * ret;

    if (NULL == (id = xmlsec1mod_unwrap_xmlSecKeyStoreId(arg)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecKeyStoreId);
        return NULL;
    }

    if (NULL == (ptr = xmlSecKeyStoreCreate(id)))
    {
        ENSURE_EXCEPTION_SET(xmlSecKeyStoreCreate);
        return NULL;
    }

    if (NULL == (ret = xmlsec1mod_wrap_xmlSecKeyStorePtr(ptr)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_wrap_xmlSecKeyStorePtr);
        xmlSecKeyStoreDestroy(ptr);
        return NULL;
    }

    return ret;
}



static PyObject * xmlsec1_xmlSecKeyStoreDestroy(PyObject * self, PyObject * arg)
{
    xmlSecKeyStorePtr ptr;

    if (NULL == (ptr =  xmlsec1mod_unwrap_xmlSecKeyStorePtr(arg)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecKeyStorePtr);
        return NULL;
    }

    xmlSecKeyStoreDestroy(ptr);
    Py_RETURN_NONE;
}



static PyObject * xmlsec1_xmlSecKeyStoreFindKey(PyObject * self, PyObject * args, PyObject * kwds)
{
    PyObject * arg1;
    PyObject * arg2;
    PyObject * arg3;
    xmlSecKeyStorePtr store;
    xmlChar * name;
    xmlSecKeyInfoCtxPtr keyInfoCtx;
    xmlSecKeyPtr ptr;
    PyObject * ret;

    static char * kwlist[] = {"store", "name", "keyInfoCtx", NULL};

    arg1 = arg2 = arg3 = NULL;
    store = NULL;
    name = NULL;
    keyInfoCtx = NULL;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOO", kwlist, &arg1, &arg2, &arg3))
    {
        return NULL;
    }

    if (NULL == (store = xmlsec1mod_unwrap_xmlSecKeyStorePtr(arg1)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecKeyStorePtr);
        return NULL;
    }

    if (NULL == (keyInfoCtx = xmlsec1mod_unwrap_xmlSecKeyInfoCtxPtr(arg3)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecKeyInfoCtxPtr);
        return NULL;
    }

    if (0 != xmlsec1_UnicodeOrBytesToXmlCharPtr(arg2, &name, NULL))
    {
        ENSURE_EXCEPTION_SET(xmlsec1_UnicodeOrBytesToXmlCharPtr);
        return NULL;
    }

    if (NULL == (ptr = xmlSecKeyStoreFindKey(store, name, keyInfoCtx)))
    {
        xmlFree(name);

        if (!PyErr_Occurred())
        {
            Py_RETURN_NONE;
        }

        ENSURE_EXCEPTION_SET(xmlSecKeyStoreFindKey);
        return NULL;
    }

    xmlFree(name);

    if (NULL == (ret = xmlsec1mod_wrap_xmlSecKeyPtr(ptr)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_wrap_xmlSecKeyPtr);
        xmlSecKeyDestroy(ptr);
        return NULL;
    }

    return ret;
}



static PyObject * xmlsec1_xmlSecKeyStoreGetName(PyObject * self, PyObject * arg)
{
    xmlSecKeyStorePtr store;

    if (NULL == (store = xmlsec1mod_unwrap_xmlSecKeyStorePtr(arg)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecKeyStorePtr);
        return NULL;
    }

    return PyBytes_FromString((const char *)xmlSecKeyStoreGetName(store));
}



static PyObject * xmlsec1_xmlSecKeyStoreIsValid(PyObject * self, PyObject * arg)
{
    xmlSecKeyStorePtr store;

    if (NULL == (store = xmlsec1mod_unwrap_xmlSecKeyStorePtr(arg)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecKeyStorePtr);
        return NULL;
    }

    if (xmlSecKeyStoreIsValid(store))
    {
        Py_RETURN_TRUE;
    }

    Py_RETURN_FALSE;
}



static PyObject * xmlsec1_xmlSecKeyStoreCheckId(PyObject * self, PyObject * args, PyObject * kwds)
{
    PyObject * arg1;
    PyObject * arg2;
    xmlSecKeyStorePtr store;
    xmlSecKeyStoreId storeId;

    static char * kwlist[] = {"store", "storeId", NULL};

    arg1 = arg2 = NULL;
    store = NULL;
    storeId = NULL;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist, &arg1, &arg2))
    {
        return NULL;
    }

    if (NULL == (store = xmlsec1mod_unwrap_xmlSecKeyStorePtr(arg1)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecKeyStorePtr);
        return NULL;
    }

    if (NULL == (storeId = xmlsec1mod_unwrap_xmlSecKeyStoreId(arg2)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecKeyStoreId);
        return NULL;
    }

    if (xmlSecKeyStoreCheckId(store, storeId))
    {
        Py_RETURN_TRUE;
    }

    Py_RETURN_FALSE;
}



static PyObject * xmlsec1_xmlSecKeyStoreCheckSize(PyObject * self, PyObject * args, PyObject * kwds)
{
    PyObject * arg1;
    int arg2;
    xmlSecKeyStorePtr store;
    xmlSecSize size;

    static char * kwlist[] = {"store", "size", NULL};

    arg1 = NULL;
    arg2 = 0;
    store = NULL;
    size = 0;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oi", kwlist, &arg1, &arg2))
    {
        return NULL;
    }

    if (NULL == (store = xmlsec1mod_unwrap_xmlSecKeyStorePtr(arg1)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecKeyStorePtr);
        return NULL;
    }

    size = (xmlSecSize)arg2;

    if (xmlSecKeyStoreCheckSize(store, size))
    {
        Py_RETURN_TRUE;
    }

    Py_RETURN_FALSE;
}



static PyObject * xmlsec1_xmlSecKeyStoreKlassGetName(PyObject * self, PyObject * arg)
{
    xmlSecKeyStoreId klass;

    if (NULL == (klass = xmlsec1mod_unwrap_xmlSecKeyStoreId(arg)))
    {
        ENSURE_EXCEPTION_SET(xmlSecSimpleKeysStoreGetKlass);
        return NULL;
    }

    return PyBytes_FromString((const char *)xmlSecKeyStoreKlassGetName(klass));
}



static PyObject * xmlsec1_xmlSecSimpleKeysStoreGetKlass(PyObject * self, PyObject * args)
{
    xmlSecKeyStoreId klass = NULL;

    if (NULL == (klass = xmlSecSimpleKeysStoreGetKlass()))
    {
        ENSURE_EXCEPTION_SET(xmlSecSimpleKeysStoreGetKlass);
        return NULL;
    }

    return xmlsec1mod_wrap_xmlSecKeyStoreId(klass);
}



static PyObject * xmlsec1_xmlSecSimpleKeysStoreAdoptKey(PyObject * self, PyObject * args, PyObject * kwds)
{
    PyObject * arg1;
    PyObject * arg2;
    xmlSecKeyStorePtr store;
    xmlSecKeyPtr key;

    static char * kwlist[] = {"store", "key", NULL};

    arg1 = arg2 = NULL;
    store = NULL;
    key = NULL;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist, &arg1, &arg2))
    {
        return NULL;
    }

    if (NULL == (store = xmlsec1mod_unwrap_xmlSecKeyStorePtr(arg1)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecKeyStorePtr);
        return NULL;
    }

    if (NULL == (key = xmlsec1mod_unwrap_xmlSecKeyPtr(arg2)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecKeyPtr);
        return NULL;
    }

    if (0 != xmlSecSimpleKeysStoreAdoptKey(store, key))
    {
        ENSURE_EXCEPTION_SET(xmlSecSimpleKeysStoreAdoptKey);
        return NULL;
    }

    Py_RETURN_NONE;
}



static PyObject * xmlsec1_xmlSecSimpleKeysStoreLoad(PyObject * self, PyObject * args, PyObject * kwds)
{
    PyObject * arg1;
    PyObject * arg2;
    PyObject * arg3;
    xmlSecKeyStorePtr store;
    xmlSecKeysMngrPtr keysMngr;
    PyObject * tmp;

    static char * kwlist[] = {"store", "uri", "keysMngr", NULL};

    arg1 = arg2 = arg3 = NULL;
    store = NULL;
    keysMngr = NULL;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOO", kwlist, &arg1, &arg2, &arg3))
    {
        return NULL;
    }

    if (NULL == (store = xmlsec1mod_unwrap_xmlSecKeyStorePtr(arg1)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecKeyStorePtr);
        return NULL;
    }

    if (NULL == (keysMngr = xmlsec1mod_unwrap_xmlSecKeysMngrPtr(arg3)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecKeysMngrPtr);
        return NULL;
    } 

    if (PyUnicode_Check(arg2))
    {
        if (NULL == (tmp = PyUnicode_AsUTF8String(arg2)))
        {
            ENSURE_EXCEPTION_SET(PyUnicode_AsUTF8String);
            return NULL;
        }

        if (0 != xmlSecSimpleKeysStoreLoad(store, PyBytes_AsString(tmp), keysMngr))
        {
            ENSURE_EXCEPTION_SET(xmlSecSimpleKeysStoreLoad);
            Py_DECREF(tmp);
            return NULL;
        }

        Py_DECREF(tmp);
    }
    else if (PyBytes_Check(arg2))
    {
        if (0 != xmlSecSimpleKeysStoreLoad(store, PyBytes_AsString(arg2), keysMngr))
        {
            ENSURE_EXCEPTION_SET(xmlSecSimpleKeysStoreLoad);
            return NULL;
        }
    }
    else
    {
        PyErr_SetString(PyExc_TypeError, "input must be of typpe bytes (string) or unicode");
        return NULL;
    }

    Py_RETURN_NONE;
}



static PyObject * xmlsec1_xmlSecSimpleKeysStoreSave(PyObject * self, PyObject * args, PyObject * kwds)
{
    PyObject * arg1;
    PyObject * arg2;
    int arg3;
    xmlSecKeyStorePtr store;
    xmlSecKeyDataType type;
    PyObject * tmp;

    static char * kwlist[] = {"store", "filename", "ttype", NULL};

    arg1 = arg2 = NULL;
    store = NULL;
    type = 0;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOi", kwlist, &arg1, &arg2, &arg3))
    {
        return NULL;
    }

    if (NULL == (store = xmlsec1mod_unwrap_xmlSecKeyStorePtr(arg1)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecKeyStorePtr);
        return NULL;
    }

    type = (xmlSecKeyDataType)arg3;

    if (PyUnicode_Check(arg2))
    {
        if (NULL == (tmp = PyUnicode_AsUTF8String(arg2)))
        {
            ENSURE_EXCEPTION_SET(PyUnicode_AsUTF8String);
            return NULL;
        }

        if (0 != xmlSecSimpleKeysStoreSave(store, PyBytes_AsString(tmp), type))
        {
            ENSURE_EXCEPTION_SET(xmlSecSimpleKeysStoreSave);
            Py_DECREF(tmp);
            return NULL;
        }

        Py_DECREF(tmp);
    }
    else if (PyBytes_Check(arg2))
    {
        if (0 != xmlSecSimpleKeysStoreSave(store, PyBytes_AsString(arg2), type))
        {
            ENSURE_EXCEPTION_SET(xmlSecSimpleKeysStoreSave);
            return NULL;
        }
    }
    else
    {
        PyErr_SetString(PyExc_TypeError, "input must be of typpe bytes (string) or unicode");
        return NULL;
    }

    Py_RETURN_NONE;
}



static PyObject * xmlsec1_xmlSecSimpleKeysStoreGetKeys(PyObject * self, PyObject * arg)
{
    xmlSecKeyStorePtr store;
    xmlSecPtrListPtr list;
    PyObject * ret;

    if (NULL == (store = xmlsec1mod_unwrap_xmlSecKeyStorePtr(arg)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecKeyStorePtr);
        return NULL;
    }

    if (NULL == (list = xmlSecSimpleKeysStoreGetKeys(store)))
    {
        ENSURE_EXCEPTION_SET(xmlSecSimpleKeysStoreGetKeys);
        return NULL;
    }

    if (NULL == (ret = xmlsec1mod_wrap_xmlSecPtrListPtr(list)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_wrap_xmlSecPtrListPtr);
        return NULL; /* XXX: need to check if we own the memory in list */
    }

    return ret;
}



#if 0
-- xmlSecKeysMngrPtr xmlSecKeysMngrCreate(void);
-- void xmlSecKeysMngrDestroy(xmlSecKeysMngrPtr mngr);
-- xmlSecKeyPtr xmlSecKeysMngrFindKey(xmlSecKeysMngrPtr mngr, const xmlChar *name, xmlSecKeyInfoCtxPtr keyInfoCtx);
-- int xmlSecKeysMngrAdoptKeysStore(xmlSecKeysMngrPtr mngr, xmlSecKeyStorePtr store);
-- xmlSecKeyStorePtr xmlSecKeysMngrGetKeysStore(xmlSecKeysMngrPtr mngr);
-- int xmlSecKeysMngrAdoptDataStore(xmlSecKeysMngrPtr mngr, xmlSecKeyDataStorePtr store);
-- xmlSecKeyDataStorePtr xmlSecKeysMngrGetDataStore(xmlSecKeysMngrPtr mngr, xmlSecKeyDataStoreId id);
-- xmlSecKeyPtr (*xmlSecGetKeyCallback)(xmlNodePtr keyInfoNode, xmlSecKeyInfoCtxPtr keyInfoCtx);
-- struct xmlSecKeysMngr;
-- xmlSecKeyPtr xmlSecKeysMngrGetKey(xmlNodePtr keyInfoNode, xmlSecKeyInfoCtxPtr keyInfoCtx);

-- struct xmlSecKeyStore;
-- xmlSecKeyStorePtr xmlSecKeyStoreCreate(xmlSecKeyStoreId id);
-- void xmlSecKeyStoreDestroy(xmlSecKeyStorePtr store);
-- xmlSecKeyPtr xmlSecKeyStoreFindKey(xmlSecKeyStorePtr store, const xmlChar *name, xmlSecKeyInfoCtxPtr keyInfoCtx);
-- #define xmlSecKeyStoreGetName(store)
-- #define xmlSecKeyStoreIsValid(store)
-- #define xmlSecKeyStoreCheckId(store, storeId)
-- #define xmlSecKeyStoreCheckSize(store, size)
#define xmlSecKeyStoreIdUnknown
-- int (*xmlSecKeyStoreInitializeMethod)(xmlSecKeyStorePtr store);
-- void (*xmlSecKeyStoreFinalizeMethod)(xmlSecKeyStorePtr store);
-- xmlSecKeyPtr (*xmlSecKeyStoreFindKeyMethod)(xmlSecKeyStorePtr store, const xmlChar *name, xmlSecKeyInfoCtxPtr keyInfoCtx);
-- struct xmlSecKeyStoreKlass;
-- #define xmlSecKeyStoreKlassGetName(klass)
-- #define xmlSecSimpleKeysStoreId
-- xmlSecKeyStoreId xmlSecSimpleKeysStoreGetKlass(void);
-- int xmlSecSimpleKeysStoreAdoptKey(xmlSecKeyStorePtr store, xmlSecKeyPtr key);
-- int xmlSecSimpleKeysStoreLoad(xmlSecKeyStorePtr store, const char *uri, xmlSecKeysMngrPtr keysMngr);
-- int xmlSecSimpleKeysStoreSave(xmlSecKeyStorePtr store, const char *filename, xmlSecKeyDataType type);
-- xmlSecPtrListPtr xmlSecSimpleKeysStoreGetKeys(xmlSecKeyStorePtr store);
#endif

#endif
